<?php
require_once(PATH_TO_APP . "/components/tags/Tags.php");
require_once(PATH_TO_APP . "/components/tags/tabledatagateway/TagsGateway.php");

class Search
{
    private $output_handler_;
    private $request_;
    private $table_gateway_;
    private $session_;

    public function __construct($table_gateway, $singleton_registry)
    {
        $this->table_gateway_ = $table_gateway;
        $this->output_handler_ = $singleton_registry->getSingleton('OutputHandler');
        $this->request_ = $singleton_registry->getSingleton('Request');
        $this->session_ = $singleton_registry->getSingleton('Session');
    }
    
    public function __destruct(){}
    
    public function search()
    {
        switch($this->request_->getParameter('get', 'type'))
        {
            case 'tag':
                $this->getDataByTagId($this->request_->getParameter('get', 'tag_id'));
                break;  
            case 'all':
            $this->searchAll();
                break;
            default:
                  $this->showSearchForm();
        }
    }
    
    private function getDataByTagId($tag_id)
    {
        $tags = new Tags(new TagsGateway());
        $tag_name = $tags->getTagNameById($tag_id);
        $arr_data = array();
        if($tag_name !== false)
            $arr_data = $this->table_gateway_->getDataTablesByTagId($tag_id);
        $layer = new Layer(new LayerGateway);
        $region = new Region(new RegionGateway);
        $topic = new Topic(new TopicGateway());
        $this->output_handler_->assignValuesToTemplate(array("search_value" => $tag_name, "arr_datatables" => $arr_data, "arr_layers" => $layer->getLayers(), "arr_regions" => $region->getRegions(), "arr_topics" => $topic->getTopics(), "arr_topics_global" => $tags->getTagCategories()));
        $this->output_handler_->showTemplate("search");
    }
    
    private function searchAll()
    {
        $arr_s_values = array();
        $arr_s_values['title'] = trim(strip_tags($this->request_->getParameter('get', 'search_title')));
        $arr_s_values['tag_id'] = (int)$this->request_->getParameter('get', 'tag');
        $arr_s_values['layer_id'] = (int)$this->request_->getParameter('get', 'layer');
        $arr_s_values['region_id'] = (int)$this->request_->getParameter('get', 'region');
        $arr_s_values['globaltopic'] = (int)$this->request_->getParameter('get', 'globaltopic');
        $this->output_handler_->assignValuesToTemplate(array("arr_datatables" => $this->table_gateway_->getSearchResult($arr_s_values), "arr_s_values" => $arr_s_values));
        $this->showSearchForm();
    }
    
    private function showSearchForm()
    {
        $tags = new Tags(new TagsGateway());
        $layer = new Layer(new LayerGateway());
        $region = new Region(new RegionGateway);
        $this->output_handler_->assignValuesToTemplate(array("arr_tags" => $tags->getAllTagsByFamily(), "arr_layers" => $layer->getLayers(), "arr_regions" => $region->getRegions(), "arr_topics_global" => $tags->getTagCategories(), "search_all" => true));
        $this->output_handler_->showTemplate("search");
    }
}
?>
